<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>errorhandler_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_debug_errorhandler_test.js.html">errorhandler_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.debug.ErrorHandlerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.debug.ErrorHandlerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.MockControl&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>var oldGetObjectByName;
<a name="line23"></a>
<a name="line24"></a>// provide our own window that implements our instrumented and
<a name="line25"></a>// immediate-call versions of setTimeout and setInterval
<a name="line26"></a>var fakeWin = {};
<a name="line27"></a>
<a name="line28"></a>var errorHandler;
<a name="line29"></a>var mockControl;
<a name="line30"></a>
<a name="line31"></a>function badTimer() {
<a name="line32"></a>  arguments.callee.called = true;
<a name="line33"></a>  throw &#39;die die die&#39;;
<a name="line34"></a>}
<a name="line35"></a>
<a name="line36"></a>function setUp() {
<a name="line37"></a>  mockControl = new goog.testing.MockControl();
<a name="line38"></a>  // On IE, globalEval happens async. So make it synchronous.
<a name="line39"></a>  goog.globalEval = function(str) {
<a name="line40"></a>    eval(str);
<a name="line41"></a>  };
<a name="line42"></a>
<a name="line43"></a>  oldGetObjectByName = goog.getObjectByName;
<a name="line44"></a>  goog.getObjectByName = function(name) {
<a name="line45"></a>    if (name == &#39;window&#39;) {
<a name="line46"></a>      return fakeWin;
<a name="line47"></a>    } else {
<a name="line48"></a>      return oldGetObjectByName(name);
<a name="line49"></a>    }
<a name="line50"></a>  };
<a name="line51"></a>
<a name="line52"></a>  fakeWin.setTimeout = function(fn, time) {
<a name="line53"></a>    fakeWin.setTimeout.called = true;
<a name="line54"></a>    fakeWin.setTimeout.that = this;
<a name="line55"></a>    if (goog.isString(fn)) {
<a name="line56"></a>      eval(fn);
<a name="line57"></a>    } else {
<a name="line58"></a>      fn();
<a name="line59"></a>    }
<a name="line60"></a>  };
<a name="line61"></a>
<a name="line62"></a>  fakeWin.setInterval = function(fn, time) {
<a name="line63"></a>    fakeWin.setInterval.called = true;
<a name="line64"></a>    fakeWin.setInterval.that = this;
<a name="line65"></a>    if (goog.isString(fn)) {
<a name="line66"></a>      eval(fn);
<a name="line67"></a>    } else {
<a name="line68"></a>      fn();
<a name="line69"></a>    }
<a name="line70"></a>  };
<a name="line71"></a>
<a name="line72"></a>  fakeWin.requestAnimationFrame = function(fn) {
<a name="line73"></a>    fakeWin.requestAnimationFrame.called = true;
<a name="line74"></a>    fakeWin.requestAnimationFrame.that = this;
<a name="line75"></a>    fn();
<a name="line76"></a>  };
<a name="line77"></a>
<a name="line78"></a>  // just record the exception in the error handler when it happens
<a name="line79"></a>  errorHandler = new goog.debug.ErrorHandler(
<a name="line80"></a>      function(ex) { this.ex = ex; });
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>function tearDown() {
<a name="line84"></a>  mockControl.$tearDown();
<a name="line85"></a>  goog.dispose(errorHandler);
<a name="line86"></a>  errorHandler = null;
<a name="line87"></a>
<a name="line88"></a>  goog.getObjectByName = oldGetObjectByName;
<a name="line89"></a>
<a name="line90"></a>  delete badTimer[&#39;__protected__&#39;];
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testWrapSetTimeout() {
<a name="line94"></a>  errorHandler.protectWindowSetTimeout();
<a name="line95"></a>
<a name="line96"></a>  var caught;
<a name="line97"></a>
<a name="line98"></a>  try {
<a name="line99"></a>    fakeWin.setTimeout(badTimer, 3);
<a name="line100"></a>  } catch (ex) {
<a name="line101"></a>    caught = ex;
<a name="line102"></a>  }
<a name="line103"></a>  assertSetTimeoutError(caught);
<a name="line104"></a>}
<a name="line105"></a>
<a name="line106"></a>function testWrapSetTimeoutWithString() {
<a name="line107"></a>  errorHandler.protectWindowSetTimeout();
<a name="line108"></a>
<a name="line109"></a>  var caught;
<a name="line110"></a>
<a name="line111"></a>  try {
<a name="line112"></a>    fakeWin.setTimeout(&#39;badTimer()&#39;, 3);
<a name="line113"></a>  } catch (ex) {
<a name="line114"></a>    caught = ex;
<a name="line115"></a>  }
<a name="line116"></a>  assertSetTimeoutError(caught);
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>function testWrapSetInterval() {
<a name="line120"></a>  errorHandler.protectWindowSetInterval();
<a name="line121"></a>
<a name="line122"></a>  var caught;
<a name="line123"></a>
<a name="line124"></a>  try {
<a name="line125"></a>    fakeWin.setInterval(badTimer, 3);
<a name="line126"></a>  } catch (ex) {
<a name="line127"></a>    caught = ex;
<a name="line128"></a>  }
<a name="line129"></a>  assertSetIntervalError(caught);
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testWrapSetIntervalWithString() {
<a name="line133"></a>  errorHandler.protectWindowSetInterval();
<a name="line134"></a>
<a name="line135"></a>  var caught;
<a name="line136"></a>
<a name="line137"></a>  try {
<a name="line138"></a>    fakeWin.setInterval(&#39;badTimer()&#39;, 3);
<a name="line139"></a>  } catch (ex) {
<a name="line140"></a>    caught = ex;
<a name="line141"></a>  }
<a name="line142"></a>  assertSetIntervalError(caught);
<a name="line143"></a>}
<a name="line144"></a>
<a name="line145"></a>function testWrapRequestAnimationFrame() {
<a name="line146"></a>  errorHandler.protectWindowRequestAnimationFrame();
<a name="line147"></a>
<a name="line148"></a>  var caught;
<a name="line149"></a>  try {
<a name="line150"></a>    fakeWin.requestAnimationFrame(badTimer);
<a name="line151"></a>  } catch (ex) {
<a name="line152"></a>    caught = ex;
<a name="line153"></a>  }
<a name="line154"></a>  assertRequestAnimationFrameError(caught);
<a name="line155"></a>}
<a name="line156"></a>
<a name="line157"></a>function testDisposal() {
<a name="line158"></a>  fakeWin = goog.getObjectByName(&#39;window&#39;);
<a name="line159"></a>  var originalSetTimeout = fakeWin.setTimeout;
<a name="line160"></a>  var originalSetInterval = fakeWin.setInterval;
<a name="line161"></a>
<a name="line162"></a>  errorHandler.protectWindowSetTimeout();
<a name="line163"></a>  errorHandler.protectWindowSetInterval();
<a name="line164"></a>
<a name="line165"></a>  assertNotEquals(originalSetTimeout, fakeWin.setTimeout);
<a name="line166"></a>  assertNotEquals(originalSetInterval, fakeWin.setInterval);
<a name="line167"></a>
<a name="line168"></a>  errorHandler.dispose();
<a name="line169"></a>
<a name="line170"></a>  assertEquals(originalSetTimeout, fakeWin.setTimeout);
<a name="line171"></a>  assertEquals(originalSetInterval, fakeWin.setInterval);
<a name="line172"></a>}
<a name="line173"></a>
<a name="line174"></a>function testUnwrap() {
<a name="line175"></a>  var fn = function() {};
<a name="line176"></a>  var wrappedFn = errorHandler.wrap(fn);
<a name="line177"></a>  assertNotEquals(wrappedFn, fn);
<a name="line178"></a>
<a name="line179"></a>  assertEquals(fn, errorHandler.unwrap(fn));
<a name="line180"></a>  assertEquals(fn, errorHandler.unwrap(wrappedFn));
<a name="line181"></a>}
<a name="line182"></a>
<a name="line183"></a>function testStackPreserved() {
<a name="line184"></a>  var e;
<a name="line185"></a>  var hasStacks;
<a name="line186"></a>  function specialFunctionName() {
<a name="line187"></a>    var e = Error();
<a name="line188"></a>    hasStacks = !!e.stack;
<a name="line189"></a>    throw e;
<a name="line190"></a>  };
<a name="line191"></a>  var wrappedFn = errorHandler.wrap(specialFunctionName);
<a name="line192"></a>  try {
<a name="line193"></a>    wrappedFn();
<a name="line194"></a>  } catch (exception) {
<a name="line195"></a>    e = exception;
<a name="line196"></a>  }
<a name="line197"></a>  assertTrue(!!e);
<a name="line198"></a>  if (hasStacks) {
<a name="line199"></a>    assertContains(&#39;specialFunctionName&#39;, e.stack);
<a name="line200"></a>  }
<a name="line201"></a>}
<a name="line202"></a>
<a name="line203"></a>function testGetProtectedFunction() {
<a name="line204"></a>  var fn = function() {
<a name="line205"></a>    throw new Error(&#39;Foo&#39;);
<a name="line206"></a>  };
<a name="line207"></a>  var protectedFn = errorHandler.getProtectedFunction(fn);
<a name="line208"></a>  var e = assertThrows(protectedFn);
<a name="line209"></a>  assertTrue(e instanceof goog.debug.ErrorHandler.ProtectedFunctionError);
<a name="line210"></a>  assertEquals(&#39;Foo&#39;, e.cause.message);
<a name="line211"></a>}
<a name="line212"></a>
<a name="line213"></a>function testGetProtectedFunction_withoutWrappedErrors() {
<a name="line214"></a>  var shouldCallErrorLog = !!Error.captureStackTrace;
<a name="line215"></a>  if (shouldCallErrorLog) {
<a name="line216"></a>    mockControl.createMethodMock(goog.global.console,
<a name="line217"></a>        &#39;error&#39;);
<a name="line218"></a>  }
<a name="line219"></a>  errorHandler.setWrapErrors(false);
<a name="line220"></a>  var fn = function() {
<a name="line221"></a>    var e = new Error(&#39;Foo&#39;);
<a name="line222"></a>    e.stack = &#39;STACK&#39;;
<a name="line223"></a>    throw e;
<a name="line224"></a>  };
<a name="line225"></a>  var protectedFn = errorHandler.getProtectedFunction(fn);
<a name="line226"></a>  if (shouldCallErrorLog) {
<a name="line227"></a>    goog.global.console.error(&#39;Foo&#39;, &#39;STACK&#39;);
<a name="line228"></a>  }
<a name="line229"></a>  mockControl.$replayAll();
<a name="line230"></a>  var e = assertThrows(protectedFn);
<a name="line231"></a>  mockControl.$verifyAll();
<a name="line232"></a>  assertTrue(e instanceof Error);
<a name="line233"></a>  assertEquals(&#39;Foo&#39;, e.message);
<a name="line234"></a>  assertEquals(e.stack, &#39;STACK&#39;);
<a name="line235"></a>}
<a name="line236"></a>
<a name="line237"></a>function testGetProtectedFunction_withoutWrappedErrorsWithMessagePrefix() {
<a name="line238"></a>  errorHandler.setWrapErrors(false);
<a name="line239"></a>  errorHandler.setPrefixErrorMessages(true);
<a name="line240"></a>  var fn = function() {
<a name="line241"></a>    throw new Error(&#39;Foo&#39;);
<a name="line242"></a>  };
<a name="line243"></a>  var protectedFn = errorHandler.getProtectedFunction(fn);
<a name="line244"></a>  var e = assertThrows(protectedFn);
<a name="line245"></a>  assertTrue(e instanceof Error);
<a name="line246"></a>  assertEquals(
<a name="line247"></a>      goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX +
<a name="line248"></a>          &#39;Foo&#39;, e.message);
<a name="line249"></a>
<a name="line250"></a>  var stringError = function() {
<a name="line251"></a>    throw &#39;String&#39;;
<a name="line252"></a>  };
<a name="line253"></a>  protectedFn = errorHandler.getProtectedFunction(stringError);
<a name="line254"></a>  e = assertThrows(protectedFn);
<a name="line255"></a>  assertEquals(&#39;string&#39;, typeof e);
<a name="line256"></a>  assertEquals(
<a name="line257"></a>      goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX +
<a name="line258"></a>          &#39;String&#39;, e);
<a name="line259"></a>}
<a name="line260"></a>
<a name="line261"></a>function assertSetTimeoutError(caught) {
<a name="line262"></a>  assertMethodCalledHelper(&#39;setTimeout&#39;, caught);
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>function assertSetIntervalError(caught) {
<a name="line266"></a>  assertMethodCalledHelper(&#39;setInterval&#39;, caught);
<a name="line267"></a>}
<a name="line268"></a>
<a name="line269"></a>function assertRequestAnimationFrameError(caught) {
<a name="line270"></a>  assertMethodCalledHelper(&#39;requestAnimationFrame&#39;, caught);
<a name="line271"></a>}
<a name="line272"></a>
<a name="line273"></a>function assertMethodCalledHelper(method, caught) {
<a name="line274"></a>  assertTrue(&#39;exception not thrown&#39;, !!caught);
<a name="line275"></a>  assertEquals(&#39;exception not caught by error handler&#39;,
<a name="line276"></a>      caught.cause, errorHandler.ex);
<a name="line277"></a>  assertTrue(&#39;fake &#39; + method + &#39; not called&#39;,
<a name="line278"></a>      !!fakeWin[method].called);
<a name="line279"></a>  assertTrue(&#39;&quot;this&quot; not passed to original &#39; + method,
<a name="line280"></a>      fakeWin[method].that === fakeWin);
<a name="line281"></a>}
</pre>


</body>
</html>
